import 'dart:io';

import 'package:device_info/device_info.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:package_info/package_info.dart';

class PackagePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _PackagePage();
  }
}

class _PackagePage extends State<PackagePage> {
  String _packageStr = '';
  String _deviceStr = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('应用/设备信息'),
      ),
      body: _body(),
    );
  }

  Widget _body() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        OutlineButton(
          child: Text('应用信息'),
          onPressed: _packageInfo,
        ),
        Container(
          padding: EdgeInsets.all(10),
          child: Text(_packageStr),
        ),
        OutlineButton(
          child: Text('设备信息'),
          onPressed: _deviceInfo,
        ),
        Container(
          padding: EdgeInsets.all(10),
          child: Text(_deviceStr),
        ),
      ],
    );
  }

  _packageInfo() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    setState(() {
      _packageStr = '\nappName=${packageInfo.appName}';
      _packageStr += '\npackageName=${packageInfo.packageName}';
      _packageStr += '\nversion=${packageInfo.version}';
      _packageStr += '\nbuildNumber=${packageInfo.buildNumber}';
    });
  }

  _deviceInfo() async {
    DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
    if (Platform.isAndroid) {
      AndroidDeviceInfo androidDeviceInfo = await deviceInfoPlugin.androidInfo;
      _deviceStr = '\n version=${androidDeviceInfo.version}';
      _deviceStr += '\n model=${androidDeviceInfo.model}';
      _deviceStr += '\n androidId=${androidDeviceInfo.androidId}';
      _deviceStr += '\n board=${androidDeviceInfo.board}';
      _deviceStr += '\n brand=${androidDeviceInfo.brand}';
      _deviceStr += '\n device=${androidDeviceInfo.device}';
      _deviceStr += '\n display=${androidDeviceInfo.display}';
      _deviceStr += '\n fingerprint=${androidDeviceInfo.fingerprint}';
      _deviceStr += '\n id=${androidDeviceInfo.id}';
      _deviceStr += '\n bootloader=${androidDeviceInfo.bootloader}';
      _deviceStr += '\n hardware=${androidDeviceInfo.hardware}';
      _deviceStr += '\n host=${androidDeviceInfo.host}';
      _deviceStr += '\n isPhysicalDevice=${androidDeviceInfo.isPhysicalDevice}';
      _deviceStr += '\n manufacturer=${androidDeviceInfo.manufacturer}';
      _deviceStr += '\n product=${androidDeviceInfo.product}';
      print('device:${androidDeviceInfo.toString()}');
      setState(() {});
    } else if (Platform.isIOS) {
      IosDeviceInfo iosDeviceInfo = await deviceInfoPlugin.iosInfo;
      _deviceStr = '\n systemVersion=${iosDeviceInfo.systemVersion}';
      _deviceStr += '\n model=${iosDeviceInfo.model}';
      _deviceStr += '\n systemName=${iosDeviceInfo.systemName}';
      _deviceStr += '\n version=${iosDeviceInfo.utsname.version}';
      _deviceStr += '\n machine=${iosDeviceInfo.utsname.machine}';
      _deviceStr += '\n nodename=${iosDeviceInfo.utsname.nodename}';
      _deviceStr += '\n release=${iosDeviceInfo.utsname.release}';
      setState(() {});
    }
  }
}
